/**
 * Created by Leslie on 2017/5/22.
 */
var express = require('express');
// 创建路由，在这里担当了Controller的角色
var router = express.Router();

// 引入user模型
var usersModel = require('../models/users');

router.post('/exit', function (req, res) {
    var email = req.body.email;
    usersModel.exit(email, function (msg) {
        if (msg) {
            res.status(200).json({status: true, msg: "该用户已存在"});
        }
    });
});

router.post('/login', function (req, res) {
    var user = req.body;
    var userN = req.body.email;
    // 通过模型实现登录
    req.session.username = userN;
    usersModel.login(user, function (msg) {
        if (msg) {
            res.status(200).json({status: true, msg: '登录成功',data:req.session.username});
        } else {
            res.status(200).json({status: false, msg: msg});
        }
    });
});

router.post('/register', function (req, res) {
    var email = req.body;
    console.log(email);
    usersModel.addUser(email, function (msg) {
        if (msg) {
            res.status(200).json({status: true, msg: "注册成功"});
        }
    });
});

// 获取session
router.get('/getUser', function (req, res) {
    req.session.username = "Jerry";
    res.send("<h1>"+req.session.username+"</h1>")
});

// 保存session
/*router.get('/',function (req, res) {
    req.session.username = "Jerry";
    console.log('hello ');
    res.send("<h1>Hello </h1>");
});*/


module.exports = router;